/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package IT.Servlet;

import IT.DataAccess.DatUbigeo;
import IT.DataAccess.IDatUbigeo;
import IT.Entities.Ubigeo;
import java.io.*;
import java.net.*;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author wdelrio
 */
public class UbicacionServlet extends HttpServlet {
  

  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=ISO-8859-1");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet UbicacionServlet</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet UbicacionServlet at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
            IDatUbigeo oDatUbigeo=new DatUbigeo();
            List<Ubigeo> oLista=new ArrayList<Ubigeo>();
            String cDepartamento,cProvincia;
            cDepartamento="";cProvincia="";
            if(request.getParameter("nUbigeo")!=null){
                oLista=oDatUbigeo.ListarDepartamento();
                out.printf("<option value='%1s'>%2s</option>", "0", "[Seleccione]");
                for(Ubigeo ent: oLista){
                out.printf("<option value='%1s'>%2s</option>", ent.getCod_ubi_ch(), ent.getDes_ubi_vc());
                }
            }
            
            if(request.getParameter("cDepartamento")!=null){
               cDepartamento=request.getParameter("cDepartamento").trim();
               cDepartamento=cDepartamento.substring(0,2);               
               oLista=oDatUbigeo.ListarProvincia(cDepartamento);
                out.printf("<option value='%1s'>%2s</option>", "0", "[Seleccione]");
               for(Ubigeo ent: oLista){
                out.printf("<option value='%1s'>%2s</option>", ent.getCod_ubi_ch(), ent.getDes_ubi_vc());
               }
            }
            
            if(request.getParameter("cProvincia")!=null){
               cProvincia=request.getParameter("cProvincia").trim();
               cProvincia=cProvincia.substring(0,4);
               oLista=oDatUbigeo.ListarDistrito(cProvincia);
                out.printf("<option value='%1s'>%2s</option>", "0", "[Seleccione]");
               for(Ubigeo ent: oLista){
                out.printf("<option value='%1s'>%2s</option>", ent.getCod_ubi_ch(), ent.getDes_ubi_vc());
               }
            }
            
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
